// pages/index/group-details/group-details.js
const request = require("../../../../../utils/request");
const common = require('../../../../../utils/common')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // status == 0 拼团失败
        // status == 1 拼团详情
        // status == 2 拼团成功
        // status == 3 拼团人数已满
        // status == 4 已参团
        status: 1,
        pintuan_id: '',
        pintuanInfo:{},
        likeGoods:[]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            pintuan_id: options.id
        })
    },
    getInfo() {
        let _this = this
        request._post('/api?method=merchants.pintuanInfo', {
            order_id: this.data.pintuan_id
        }).then(res => {
           
            _this.setData({
                pintuanInfo:res.data
            })
            _this.countDown(res.data.etime)
        })
    },
    // 获取猜你喜欢
    getYouLike() {
		let _this = this
		request._post('/api?method=goods.goodslist', {
			id: this.data.goods_id,
            xiaoqu:wx.getStorageSync('localXiaoqu') ? wx.getStorageSync('localXiaoqu').id : ''
		}).then(res => {
			_this.setData({
				likeGoods: res.data
			})
		})
	},
    // 去商品详情
    toDetail(e) {
        let item = e.currentTarget.dataset.item
        let id = item.goods_id
        wx.navigateTo({
            url: '/pages/home/pages/my/product-details/product-details?id='+ id,
        })
    },
    // 倒计时
    countDown(endTime) {
        if (!endTime) {
            return
        }
        let timer = setInterval(() => {
            //截止时间
            var obj = {}
            let time = endTime
            time--;
            let now = new Date().getTime();
            let diff = parseInt(time * 1000 - now / 1000);
            if (diff < 0) {
                clearInterval(timer)
                this.setData({
                    isCountDown: false
                })
            } else {
                let date = common.formattime(diff * 1000);
                this.setData({
                    hour: date.hour,
                    minute: date.minute,
                    second: date.second
                })
            }

        }, 1000)
        this.setData({
            timer
        })
    },
    // 开团
    canTuan(e) {
        let item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/pages/home/pages/my/product-details/product-details?id='+ this.data.pintuanInfo.goods_id,
        })
    },
    // 立即参团
    rightCantuan() {
        let _this = this
        request._post("/api?method=merchants.pintuanCreate",{
            product_id:this.data.pintuanInfo.goods.product_id,
            team_id:this.data.pintuanInfo.team_id
        }).then(res=>{
            // getApp().toast('参团成功')
            if(_this.data.timer) {
                clearInterval(_this.data.timer)
            }
            setTimeout(function () {
                wx.navigateTo({
                    url: '/pages/home/pages/my/confirm-order/confirm-order?id=' + res.data.card_is + '&type=2',
                })
            },1000)
        })
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getInfo()
        this.getYouLike()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})